import router from './router/index.js'
import store from "./store/index.js";
import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'
const whiteList = ['/']

router.beforeEach(async (to, from, next) => {
    Nprogress.start()
    const token = store.state.denglu.token
    if (token) {
        const hasRoles = store.getters.roles && store.getters.roles.length > 0
        if (hasRoles) {
            next()
        } else {
            const roles = await store.dispatch("denglu/getInfo", token)
            const accessedRoutes = await store.dispatch("permission/generateRoutes", roles)
            accessedRoutes.forEach(route=>{
                router.addRoute(route)
            })
            next(to.path)
            Nprogress.done()

        }
        next()
    } else {
        if (whiteList.includes(to.path)) {
            next()
        } else {
            next("/")
        }
        Nprogress.done()

    }
})
router.afterEach((to, from) => {
    Nprogress.done()
})